/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import domain.Bankaccount;
import domain.Userdom;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alberto
 */
public class BankaccountDao {

    EntityManager em;
    EntityTransaction tx;

    public BankaccountDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Bankaccount getBankaccount(Integer idB){
        Bankaccount b = (Bankaccount) em.createNativeQuery("select * from bankaccount as b where b.idBank='"+idB+"'", Bankaccount.class).getSingleResult();
        return b;
    }

    public void saveBankaccount(Bankaccount b){
        tx.begin();
        em.persist(b);
        tx.commit();
    }

    public void removeBankaccount(Bankaccount b){
        tx.begin();
        em.remove(b);
        tx.commit();
    }

    public Set<Bankaccount> getBankaccountList(Userdom user){
        Set<Bankaccount> l = new HashSet(em.createNativeQuery("select * from bankaccount as b where b.userFK='"+user.getNick()+"'", Bankaccount.class).getResultList());
        return l;
    }

    public void updateBankAccount(Bankaccount b) {
        tx.begin();
        em.createNativeQuery("update bankaccount set name='"+b.getName()+"' where idBank='"+b.getIdBank()+"' and userFK='"+b.getUserdom()+"'").executeUpdate();
        tx.commit();
    }
}
